package com.zjt.web.api;

import com.zjt.service.IStaticParameterService;
import com.zjt.service.user.IClientUserService;
import com.zjt.utils.DateUtil;
import com.zjt.utils.ImageUtil;
import com.zjt.web.api.vo.base.PictureVO;
import com.zjt.web.api.vo.base.StateVO;
import com.zjt.web.api.vo.response.UploadPicRespVO;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: xueyanbeijing
 * Date: 12-8-14
 * Time: PM6:39
 * 上传图片接口
 */

@Controller
@RequestMapping(value = "/api/upload")
public class UploadAPIController {
    private static Logger logger = LoggerFactory.getLogger(UploadAPIController.class);


    private String savePath = "/var/www/html/resource/images/"; //文件保存路径
    private String saveUrl = "http://www.zhenjiatong.com/resource/images/"; //文件路径对应的url地址
    private IClientUserService clientUserService;
    private IStaticParameterService staticParameterService;
    @Autowired
    public void setClientUserService(IClientUserService clientUserService) {
        this.clientUserService = clientUserService;
    }
    @Autowired
    public void setStaticParameterService(IStaticParameterService staticParameterService) {
        this.staticParameterService = staticParameterService;
    }

    /**
     * 上传图片文件
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "uploadpic.json", method = RequestMethod.POST)
    @ResponseBody
    public UploadPicRespVO uploadPic(HttpServletRequest request) {
        logger.info("uploadpic ...........................");
        UploadPicRespVO uploadPicResVO = new UploadPicRespVO();
        StateVO stateVO = new StateVO();
        PictureVO pictureVO = null;
        try
        {
            this.savePath = this.staticParameterService.getParameterValueByName("imagePath")+"";
            this.saveUrl = this.staticParameterService.getParameterValueByName("imageUrl")+"";
        }catch (Exception e1){
            savePath = "/var/www/html/resource/images/"; //文件保存路径
            saveUrl = "http://www.zhenjiatong.com/resource/images/"; //文件路径对应的url地址
        }
        try
        {
            String folderYMD = DateUtil.getCurrentDateStr("yyyyMMdd");
            File tempFile = new File(savePath+folderYMD);
            if (!tempFile.exists()) tempFile.mkdir();//创建文件夹
            savePath = savePath  + folderYMD;
            saveUrl = saveUrl + folderYMD;
        }catch (Exception e2)
        {
            logger.error("新建日期文夹失败："+e2.getMessage()+"savePath="+savePath+" saveUrl="+saveUrl);
        }

        try {
            InputStream is = request.getInputStream();
            byte[] picData = IOUtils.toByteArray(is);
            if (picData != null && picData.length > 0) {

                String saveFile = ImageUtil.saveImage(picData, savePath);
                String fileUrl = saveFile.replace(savePath, saveUrl).replace("\\", "/");
                pictureVO = new PictureVO();
                pictureVO.setPictureUrl(fileUrl);
                uploadPicResVO.setPictureVO(pictureVO);
            }
        } catch (Exception e) {
            stateVO.setCode(-1);
            stateVO.setMsg("系统错误！");
            logger.error("request.getInputStream() Exception !!!", e);
        }
        uploadPicResVO.setStateVO(stateVO);
        logger.info("uploadPicResVO = " + uploadPicResVO.toString());
        return uploadPicResVO;
    }
}
